<template>
    <div class="bg">
        <div id="danmu-container"></div>
        
        <div class="content-div">
            <div class="text-magic" data-word="欢迎下次光临">
                欢迎下次光临
                <div class="white"></div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { getArticlesContent, addArticles } from "@/util/request"
import { onMounted,onUnmounted, ref ,reactive} from "vue"
// 获取数据
const messages = ref([])
const newArticle = reactive({ title: '', content: '' }); // 新留言
const currentUserId = localStorage.getItem("userId");
let timer = null // 保存定时器ID
let polling = null // 轮询
onMounted( async () => {
    // 轮询 获取弹幕列表
    polling = setInterval(async() => {
        const res = await getArticlesContent()
        messages.value = res.data || []
    }, 3000);
    // 发射弹幕
    timer = setInterval(() => {
        const msg = messages.value[Math.floor(Math.random() * messages.value.length)]?.content
        if (msg) launchDanmu(msg)
    }, 500)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
    if (polling) {
    clearInterval(polling)
  }
})

function launchDanmu(text) {
    const el = document.createElement('div');
    el.className = 'danmu';
    el.innerText = text;
    let i = Math.random() * 10 + 10;
    el.style.animation = `fly ${i}s linear forwards`;
    el.style.top = Math.random() * 80 + 'vh'; // 随机高度
    document.getElementById('danmu-container').appendChild(el);

    // 动画结束后销毁弹幕
    el.addEventListener('animationend', () => {
        el.remove();
    });
}
// 发布留言
const submitArticle = async () => {
    try {
        const res = await addArticles(newArticle.title, newArticle.content, currentUserId);
        if (res.status === 0) {
            console.log("留言发布成功");
            window.location.reload();
        } else {
            console.error("发布留言失败", res.message);
        }
    } catch (error) {
        console.error("发布留言时发生错误", error);
    }
}

</script>


<!-- 弹幕 -->
<style>
.danmu {
  position: fixed;
  top: 100px;
  left: 100%;
  white-space: nowrap;
  font-size: 16px;
  color: #fff;
  text-shadow: 1px 1px 2px #000;
  animation: fly 16s linear forwards;
  z-index: 999;
}

@keyframes fly {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(-200vw);
  }
}
</style>


<style scoped>
.bg {
    width: 100%;
    height: 100%;
    margin: 0;
    background: linear-gradient(45deg, #ffc107, deeppink, #9c27b0);
    animation: hueRotate 10s infinite alternate;
    overflow: scroll;
    display: flex;
    align-items: center;      /* 垂直居中 */
    justify-content: center;  /* 水平居中 */
    overflow: hidden; /*弹幕 */
}

@keyframes hueRotate {
    100% {
        filter: hue-rotate(360deg);
    }
}

.title {
    font-size: 50px;
    text-align: center;
}
</style>

<style scoped>
.content-div {
    position: relative;
    z-index: -1;
    background-color: rgba(0,0,0,1);
    border-radius: 15px;
    /* margin: auto; */
    padding: 70px;
    /* margin-top: 100px; */
    height: auto;
    width: 40% ;
    border: 2px solid;
    overflow: hidden;
}

</style>

<style lang="scss" scoped>
.text-magic {
    position: absolute;
    margin-left: 55%;
    // margin-right: 50%;
    top: 50%;
    // left: 50%;
    transform: translate(-50%, -50%) scale(2.4);
    width: 300px;
    font-size: 36px;
    font-family: Raleway, Verdana, Arial;
    color: #fff;
}
.white {
    position: absolute;
    left: -10px;
    width: 80%;
    height: 3px;
    background: #000;
    z-index: 4;
    animation: whiteMove 3s ease-out infinite;
}

.text-magic::before {
    content: attr(data-word);
    position: absolute;
    top: 0;
    left: 0.5px;
    height: 0px;
    color: rgba(255, 255, 255, 0.9);
    overflow: hidden;
    z-index: 2;
    animation: redShadow 1s ease-in infinite;
    filter: contrast(200%);
    text-shadow: 1px 0 0 red;
}

.text-magic::after {
    content: attr(data-word);
    position: absolute;
    top: 0;
    left: -3px;
    height: 36px;
    color: rgba(255, 255, 255, 0.8);
    overflow: hidden;
    z-index: 3;
    background: rgba(0, 0, 0, 0.9);
    animation: redHeight 1.5s ease-out infinite;
    filter: contrast(200%);
    text-shadow: -1px 0 0 cyan;
    mix-blend-mode: darken;
}

@keyframes redShadow {
    20% {
        height: 32px;
    }
    60% {
        height: 6px;
    }
    100% {
        height: 42px;
    }
}

@keyframes redHeight {
    20% {
        height: 42px;
    }
    35% {
        height: 12px;
    }
    50% {
        height: 40px;
    }
    60% {
        height: 20px;
    }
    70% {
        height: 34px;
    }
    80% {
        height: 22px;
    }
    100% {
        height: 0px;
    }
}

@keyframes whiteMove {
    8% {
        top: 38px;
    }
    14% {
        top: 8px;
    }
    20% {
        top: 42px;
    }
    32% {
        top: 2px;
    }
    99% {
        top: 30px;
    }
}
</style>